﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using StockTrader.BusinessService.DataContract.Mortgate;
using Mortgage.DAL;

namespace LoanMonitor.Utils
{
    public partial class frmCustomerManager : Form
    {
        private MortgageMornitorDAL client = new MortgageMornitorDAL();

        public frmCustomerManager()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // Check for content in the text box
            if (txtAccountNo.Text == string.Empty)
            {
                MessageBox.Show("Use the browse button to search for " +
                "the file to be added.", "Missing File Name");
                return;
            }

            // Only allow the file to be added if the file is not a duplicate
            for (int i = 0; i < lstAccountNo.Items.Count; i++)
            {
                if (lstAccountNo.Items[i].ToString() == txtAccountNo.Text.ToString())
                {
                    MessageBox.Show("That file has already been added to the list.",
                        "Duplicate");
                    return;
                }
            }

            // Add the file to the listbox list
            if (txtAccountNo.Text != string.Empty)
                lstAccountNo.Items.Add(txtAccountNo.Text.ToString());

            // clear the textbox and move the focus back to the textbox
            txtAccountNo.Text = string.Empty;
            txtAccountNo.Focus();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                lstAccountNo.Items.Remove(lstAccountNo.SelectedItem);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                // make sure there are account
                if (lstAccountNo.Items.Count < 1)
                {
                    MessageBox.Show("Xin vui lòng cho biết số tài khoản", "Oops!");
                    return;
                }
                // make sure information is filled
                if (string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtIdcardNo.Text))
                {
                    MessageBox.Show("Xin vui lòng cho biết thông tin khách hàng", "Oops!");
                    return;
                }


                string customerid = client.InsertCustomer(txtName.Text, 
                        ((rdMale.Checked) ? "Nam" : "Nữ"), txtAccount.Text,
                        txtIdcardNo.Text, dtIdCardDate.Value,
                        txtIdCardIssuer.Text, txtPhoneMobile.Text, txtPhoneFix.Text, txtEmail.Text,
                        txtAddressContact.Text, txtAddressResident.Text);

                client.deleteCustomerAccount(customerid);
                for (int i = 0; i < lstAccountNo.Items.Count; i++)
                    client.InsertCustomerAccount(customerid, lstAccountNo.Items[i].ToString());

                MessageBox.Show("Đã lưu");

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            //Find in the local database
            AccountData customer = client.getCustomerByID(txtIdcardNo.Text);
            UpdateCustomerInfo(customer);

            //Find in BOSC database

            //Create new
            if (customer == null)
                MessageBox.Show("Chưa có thông tin về khách hàng này.", "Oops!");
        }

        private void UpdateCustomerInfo(AccountData customer)
        {
            if (customer != null)
            {
                txtName.Text = customer.Name;
                txtIdcardNo.Text = customer.IDcardNo;
                dtIdCardDate.Value = customer.IDcardDate;
                txtIdCardIssuer.Text = customer.IDcardIssuer;
                txtAccount.Text = customer.AccountNo;
                txtPhoneMobile.Text = customer.PhoneMobile;
                rdMale.Checked = customer.Gender.Equals("Nam");
                txtPhoneFix.Text = customer.PhoneFix;
                txtEmail.Text = customer.Email;
                txtAddressContact.Text = customer.AddressContact;
                txtAddressResident.Text = customer.AddressResident;

                List<string> accounts = client.getCustomerAccounts(customer.IDcardNo);
                lstAccountNo.Items.Clear();
                foreach (string acc in accounts)
                    lstAccountNo.Items.Add(acc);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                client.DeleteCustomerById(txtIdcardNo.Text);
                client.deleteCustomerAccount(txtIdcardNo.Text);
                MessageBox.Show("Đã xóa");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
